"""
表单切换
    在web应用中，可能会遇到iframe/frame表单嵌套的页面，如果想定位表单里边的元素，就需要对表单进行切换
"""""
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# option = webdriver.ChromeOptions()
# option.add_argument('--headless') # 无头浏览器
path = Service(executable_path=r'C:\Users\Laptop\Desktop\thirty_one\chromedriver.exe')
driver = webdriver.Chrome(service=path)
driver.implicitly_wait(10)
driver.get('https://mail.163.com')

sleep(3)
iframe = driver.find_element(By.XPATH,"/html/body/div[3]/div[3]/div[1]/div/div[3]/div[1]/div[2]/iframe") # 获取你要切换的表单的元素
driver.switch_to.frame(iframe) # 切换子表单

sleep(3)
driver.find_element(By.NAME,'email').send_keys('123')
driver.get_screenshot_as_file('截图.png')

# driver.switch_to.parent_frame() # 切换到父文档

driver.switch_to.default_content() # 切换回主文档
sleep(3)
driver.find_element(By.PARTIAL_LINK_TEXT,'企业邮箱').click() # 点击最外层的元素
sleep(3)

